<?php 

if (! defined('BASEPATH')) exit('No direct script access');
/**
 * Maneja los permisos y suscripciones.
 *
 * @package default
 * @author Ian Murray
 */
class Permission extends Model {

  public function Permission()
  {
    
  }

  function get_permissions($user_id, $subject_id) {
    $this->db->select('*')
             ->from('users_subjects')
             ->where('user_id', $user_id)
             ->where('subject_id', $subject_id);
    
    $result = $this->db->get();
    return $result->result_array();
  }
  
  function is_subscribed($user_id, $subject_id) 
  {
    $this->db->select('COUNT(*) as cuenta')
             ->from('users_subjects')
             ->where('user_id', $user_id)
             ->where('subject_id', $subject_id);
    
    $result = $this->db->get();
    $count = $result->row_array();
    
    if($count['cuenta'] == 1) {
      return true;
    }
    else {
      return false;
    }
  }
  
  function set($user_id, $subject_id, $changes)
  {
    $this->db->where('user_id', $user_id)
             ->where('subject_id', $subject_id);
             
    $this->db->update('users_subjects', $changes);
  }
  
  function associate($user_id, $subject_id, $type)
  {
    $this->db->insert('users_subjects', array(
      'user_id' => $user_id,
      'subject_id' => $subject_id,
      'type' => $type
      /* Resto de los campos por defecto de la bd */
    ));
  }
  
  function deassociate($user_id, $subject_id)
  {
    $this->db->where('user_id', $user_id)
             ->where('subject_id', $subject_id);
             
    $this->db->delete('users_subjects');
  }
}